#include<bits/stdc++.h>
using namespace std;
#define MAX 100005
int a[MAX],c[MAX],mst[20][MAX],ayi[5],winners[MAX];
int getwinner(int r,int g,int ms,int tz){
    if(mst[r][g]==1){
        int tmp=tz;
        tz=ms;
        ms=tmp;
    }
    if(a[ms]>=r)return ms;
    else return tz;
}
int allgetwinner(int ht){
    for(int i=1;i<=ht;i++){
        for(int j=1;j<=(1<<(ht-i));j++){
            int w=getwinner(i,j,winners[j*2-1],winners[j*2]);
            winners[j]=w;
        }
    }
    return winners[1];
}
int main(){
    freopen("arena.in","r",stdin);
    freopen("arena.out","w",stdout);
    int n,m;
    scanf("%d %d",&n,&m);
    for(int i=1;i<=n;i++){
        scanf("%d",a+i);
    }
    for(int i=0;i<m;i++){
        scanf("%d",c+i);
    }
    int pow=1,ht=0;
    while(pow-1<n){
        pow*=2;ht++;
    }
    char c=getchar();
    for(int i=1;i<=ht;i++){
        for(int j=1;j<=(1<<(ht-i));j++){
            while(c!='0'&&c!='1')c=getchar();
            mst[i][j]=c-'0';
            c=getchar();
        }
    }
    int t;
    scanf("%d",&t);
    for(int tt=0;tt<t;tt++){
        for(int i=0;i<4;i++){
            scanf("%d",&ayi[i]);
        }
        for(int i=0;i<n;i++){
            a[i]^=ayi[i%4];
        }
        for(int i=0;i<=n;i++){
            winners[i]=i;
        }
        int ans=allgetwinner(ht);
        printf("%d\n",ans);
        for(int i=0;i<n;i++){
            a[i]^=ayi[i%4];
        }
    }
    return 0;
}